{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fasttext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Read 509M words\n",
      "Number of words:  11343383\n",
      "Number of labels: 10\n",
      "Progress: 100.0% words/sec/thread: 6170299 lr:  0.000000 avg.loss:  0.216921 ETA:   0h 0m 0s\n"
     ]
    }
   ],
   "source": [
    "model = fasttext.train_supervised('shuf-train-fasttext.txt', dim = 16, minn = 2, loss = 'hs')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_model(\"model-16.bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "cutoff = int(len(model.get_words()) * 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 43 µs, sys: 1e+03 ns, total: 44 µs\n",
      "Wall time: 44.8 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(('__label__local-malay', '__label__socialmedia-indonesian'),\n",
       " array([0.99847621, 0.00155871]))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "model.predict(\"bodo siak\", k = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 45 µs, sys: 2 µs, total: 47 µs\n",
      "Wall time: 48.6 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(('__label__socialmedia-indonesian', '__label__local-malay'),\n",
       " array([9.99525964e-01, 5.14027080e-04]))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "model.predict(\"rada ketuaan kalo umur masih pas lebih bagus lg aishwarya\", k = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"rada ketuaan kalo umur masih pas lebih bagus lg aishwarya\"\r\n",
      "\"ada kursi gaa soalnya nunggu kamu pegel kalo sambil berdiri\"\r\n"
     ]
    }
   ],
   "source": [
    "!head -n 2 filter-twitter-malay-rojak-id.jsonl.dedup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 49 µs, sys: 0 ns, total: 49 µs\n",
      "Wall time: 50.5 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(('__label__socialmedia-indonesian',\n",
       "  '__label__local-malay',\n",
       "  '__label__standard-malay'),\n",
       " array([0.73207742, 0.2577478 , 0.01020789]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "model.predict(\"nggak ada\", k = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Progress: 100.0% words/sec/thread: 6043507 lr:  0.000000 avg.loss:  0.158656 ETA:   0h 0m 0s\n"
     ]
    }
   ],
   "source": [
    "model.quantize(input='shuf-train-fasttext.txt', retrain=True, qnorm=True, cutoff = cutoff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_model(\"model-16.ftz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cp model-16.bin ~/ssd3/fasttext-language-detection-v2/fasttext.bin\n",
    "!cp model-16.ftz ~/ssd3/fasttext-language-detection-v2/fasttext.ftz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
